package action;

import entity.Member;
import entity.Room;
import entity.Subscription;
import entity.SubscriptionDtl;
import framework.bean.factory.ObjectFactory;
import framework.mvc.annotation.Action;
import framework.mvc.annotation.Controller;
import framework.mvc.annotation.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.RoomService;
import service.SubscriptionService;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@Action(path = "/subscription")
public class SubscriptionAction {

    @Action(path = "/check/findUnacknowledged",results = @Result(path = "/WEB-INF/page/memberCenter.jsp"))
    public String findUnacknowledged(HttpServletRequest request, HttpServletResponse response){
        SubscriptionService subscriptionService= (SubscriptionService) ObjectFactory.getObject("subscriptionService");

        Member member= (Member) request.getSession().getAttribute("member");


        List<Subscription> subscriptions=subscriptionService.findUnacknowledged(member.getId());

        request.setAttribute("subscriptions",subscriptions);

        return "success";
    }

    @Action(path = "/check/findHistorical",results = @Result(path = "/WEB-INF/page/historySubscription.jsp"))
    public String findHistorical(HttpServletRequest request, HttpServletResponse response){
        SubscriptionService subscriptionService= (SubscriptionService) ObjectFactory.getObject("subscriptionService");

        Member member= (Member) request.getSession().getAttribute("member");


        List<Subscription> subscriptions=subscriptionService.findHistorical(member.getId());

        request.setAttribute("subscriptions",subscriptions);

        return "success";
    }



    //根据用户提交的信息查询可用房间
    //房间类型 预定方式 开始日期 结束日期
    //订单id
    public String search(HttpServletRequest request,HttpServletResponse response){
        RoomService roomService= (RoomService) ObjectFactory.getObject("roomService");

        //获得请求参数

        //调用业务层查询可用房间
//        List<Room> rooms=roomService.findAvailable(xxxxx);

//        request.setAttribute("rooms",rooms);
        //将房间类型 预定方式 开始日期 结束日期 订单id传到createSubscriptionDtl-2页面

        Random random=new Random();
        request.getSession().setAttribute("token",random.nextInt(9999)+"");

        return "success";
    }

    //创建明细
    public String createDetail(HttpServletRequest request,HttpServletResponse response){
        HttpSession session=request.getSession();

        //获得第一个页面中的所有参数

        String token=request.getParameter("token");
        String sessionToken= (String) session.getAttribute("token");

        //判断token是否正确，如果不正确
            //提示用户
            //retrun

        //调用业务层创建明细(多条明细)
        String[] roomIds=request.getParameterValues("roomId");
        List<SubscriptionDtl> subscriptionDtls=new ArrayList<>();
        for (String roomId : roomIds) {
            //创建明细对象
            SubscriptionDtl subscriptionDtl=new SubscriptionDtl();
//            set
            subscriptionDtls.add(subscriptionDtl);
        }
//        subscriptionService.addDetail(subscriptionDtls);


        session.removeAttribute("token");

        return "success";
    }




































}
